package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysCusService1Bi;
import com.ruoyi.system.service.ISysCusService1BiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCjWorkOrderBi;
import com.ruoyi.system.service.ISysCjWorkOrderBiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * SysCjWorkOrderBiController
 * 
 * @author SYF
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/system/SysCjWorkOrderBi")
public class SysCjWorkOrderBiController extends BaseController
{
    @Autowired
    private ISysCjWorkOrderBiService sysCjWorkOrderBiService;

    @Autowired
    private ISysCusService1BiService cusService1BiService;

    /**
     * 查询SysCjWorkOrderBi列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysCjWorkOrderBi sysCjWorkOrderBi) {
        startPage();
        List<SysCjWorkOrderBi> list = sysCjWorkOrderBiService.selectSysCjWorkOrderBiList(sysCjWorkOrderBi);
        return getDataTable(list);
    }

    /**
     * 导出SysCjWorkOrderBi列表
     */
    @Log(title = "SysCjWorkOrderBi", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCjWorkOrderBi sysCjWorkOrderBi) {
        List<SysCjWorkOrderBi> list = sysCjWorkOrderBiService.selectSysCjWorkOrderBiList(sysCjWorkOrderBi);
        ExcelUtil<SysCjWorkOrderBi> util = new ExcelUtil<SysCjWorkOrderBi>(SysCjWorkOrderBi.class);
        util.exportExcel(response, list, "SysCjWorkOrderBi数据");
    }

    /**
     * 获取SysCjWorkOrderBi详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysCjWorkOrderBiService.selectSysCjWorkOrderBiByYear(id));
    }

    /**
     * 新增SysCjWorkOrderBi
     */
    @Log(title = "SysCjWorkOrderBi", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCjWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        order.setId(uuid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysCjWorkOrderBiService.insertSysCjWorkOrderBi(order);
        if(order.getGxbh().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("根系不好"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getNbql().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("难拔起来"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getHy().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("黄叶"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getMx().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("苗小"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getMd().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("苗大"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getTqs().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("土球散"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getThpcd().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("提货批次多"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getQt().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("其他"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        return AjaxResult.success();
    }

    /**
     * 修改SysCjWorkOrderBi
     */
    @Log(title = "SysCjWorkOrderBi", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCjWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysCjWorkOrderBiService.updateSysCjWorkOrderBi(order);
        // 1.删除
        cusService1BiService.deleteCusService1ByOrderId(order.getId());
        // 2.后检查插入
        if(order.getGxbh().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("根系不好"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getNbql().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("难拔起来"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getHy().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("黄叶"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getMx().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("苗小"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getMd().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("苗大"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getTqs().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("土球散"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getThpcd().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("提货批次多"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        if(order.getQt().toString().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("种苗抽检");
            cus.setZmpz(order.getZmpl());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu("种苗抽检");
            cus.setGid("其他"); // *****
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getSskh());
            cus.setSaleman(order.getSsxs());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        return AjaxResult.success();
    }

    /**
     * 删除SysCjWorkOrderBi
     */
    @Log(title = "SysCjWorkOrderBi", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String[] id) {
        return toAjax(sysCjWorkOrderBiService.deleteSysCjWorkOrderBiByYears(id));
    }
}
